package com.bolt.report.engine.calculator;

import com.bolt.common.utils.ArrayUtil;
import com.bolt.common.utils.DateUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.report.engine.BindData;
import com.bolt.report.engine.Calculator;
import com.bolt.report.engine.fill.FillContext;
import com.bolt.report.engine.model.Cell;
import com.bolt.report.engine.model.Expression.CalculatorExpression;

import java.util.Date;

public class FormatDateCalculator implements Calculator {

    @Override
    public Object evaluate(FillContext context, BindData data, CalculatorExpression expr, Cell cell) {
        if ((!data.isMultiple())) {
            Object value = data.getData();
            Date formatDate = null ;
            if (value instanceof Date) {
                formatDate = (Date) value;
            }else if(value instanceof Long){
                formatDate = new Date((Long)value);
            }

            if (ArrayUtil.isNotEmpty(expr.getArgs())) {
                return DateUtil.formatDate(formatDate, expr.getArgs()[0]);
            } else {
                return DateUtil.formatDate(formatDate);
            }
        }
        return StrUtil.EMPTY;
    }
}